package model

import (
	"time"
)

// Notification 通知记录表
type Notification struct {
	ID         uint       `gorm:"primaryKey" json:"id"`
	UserID     uint       `gorm:"not null;index" json:"userId"`
	TemplateID *uint      `json:"templateId"`
	Type       string     `gorm:"type:enum('COURSE','CARD','SYSTEM','OTHER');not null" json:"type"`
	Channel    string     `gorm:"type:enum('INTERNAL','SMS','EMAIL','PUSH');not null" json:"channel"`
	Title      string     `gorm:"size:200;not null" json:"title"`
	Content    string     `gorm:"type:text;not null" json:"content"`
	Link       string     `gorm:"size:500" json:"link"`
	RelatedID  *uint      `json:"relatedId"`
	Status     string     `gorm:"type:enum('PENDING','SENT','FAILED','READ');default:'PENDING'" json:"status"`
	IsRead     bool       `gorm:"default:false" json:"isRead"`
	ReadAt     *time.Time `json:"readAt"`
	SentAt     *time.Time `json:"sentAt"`
	ErrorMsg   string     `gorm:"size:500" json:"errorMsg"`
	CreatedAt  time.Time  `json:"createdAt"`
	UpdatedAt  time.Time  `json:"updatedAt"`

	// 关联字段
	User     *User     `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Template *Template `gorm:"foreignKey:TemplateID" json:"template,omitempty"`
}

// Template 通知模板表
type Template struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	Code      string    `gorm:"size:50;uniqueIndex;not null" json:"code"`
	Name      string    `gorm:"size:100;not null" json:"name"`
	Type      string    `gorm:"type:enum('COURSE','CARD','SYSTEM','OTHER');not null" json:"type"`
	Channel   string    `gorm:"type:enum('INTERNAL','SMS','EMAIL','PUSH','ALL');not null" json:"channel"`
	Title     string    `gorm:"size:200;not null" json:"title"`
	Content   string    `gorm:"type:text;not null" json:"content"`
	Variables string    `gorm:"type:json" json:"variables"`
	IsActive  bool      `gorm:"default:true" json:"isActive"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

// Announcement 系统公告表
type Announcement struct {
	ID          uint       `gorm:"primaryKey" json:"id"`
	Title       string     `gorm:"size:200;not null" json:"title"`
	Content     string     `gorm:"type:text;not null" json:"content"`
	Type        string     `gorm:"type:enum('NOTICE','EVENT','MAINTENANCE','PROMOTION');not null" json:"type"`
	Priority    string     `gorm:"type:enum('LOW','NORMAL','HIGH','URGENT');default:'NORMAL'" json:"priority"`
	TargetUsers string     `gorm:"type:enum('ALL','MEMBERS','COACHES','CUSTOM');default:'ALL'" json:"targetUsers"`
	CoverImage  string     `gorm:"size:500" json:"coverImage"`
	PublishAt   *time.Time `json:"publishAt"`
	ExpireAt    *time.Time `json:"expireAt"`
	IsPublished bool       `gorm:"default:false" json:"isPublished"`
	IsTop       bool       `gorm:"default:false" json:"isTop"`
	ViewCount   int        `gorm:"default:0" json:"viewCount"`
	CreatorID   *uint      `json:"creatorId"`
	CreatedAt   time.Time  `json:"createdAt"`
	UpdatedAt   time.Time  `json:"updatedAt"`

	// 关联字段
	Creator *User `gorm:"foreignKey:CreatorID" json:"creator,omitempty"`
}

// NotificationSetting 用户订阅配置表
type NotificationSetting struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	UserID    uint      `gorm:"not null" json:"userId"`
	Channel   string    `gorm:"type:enum('INTERNAL','SMS','EMAIL','PUSH');not null" json:"channel"`
	Type      string    `gorm:"type:enum('COURSE','CARD','SYSTEM','OTHER');not null" json:"type"`
	IsEnabled bool      `gorm:"default:true" json:"isEnabled"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

// ==================== 请求/响应模型 ====================

// NotificationListReq 通知列表查询请求
type NotificationListReq struct {
	Type     string `form:"type"`
	IsRead   *bool  `form:"isRead"`
	Page     int    `form:"page"`
	PageSize int    `form:"pageSize"`
}

// NotificationListResp 通知列表响应
type NotificationListResp struct {
	List        []NotificationItem `json:"list"`
	Total       int64              `json:"total"`
	UnreadCount int                `json:"unreadCount"`
	Page        int                `json:"page"`
	PageSize    int                `json:"pageSize"`
}

// NotificationItem 通知项
type NotificationItem struct {
	ID        uint      `json:"id"`
	Type      string    `json:"type"`
	Channel   string    `json:"channel"`
	Title     string    `json:"title"`
	Content   string    `json:"content"`
	Link      string    `json:"link"`
	IsRead    bool      `json:"isRead"`
	CreatedAt time.Time `json:"createdAt"`
}

// UnreadCountResp 未读数量响应
type UnreadCountResp struct {
	Total  int            `json:"total"`
	ByType map[string]int `json:"byType"`
}

// ReadBatchReq 批量标记已读请求
type ReadBatchReq struct {
	NotificationIDs []uint `json:"notificationIds" binding:"required,min=1"`
}

// AnnouncementListReq 公告列表查询请求
type AnnouncementListReq struct {
	Type     string `form:"type"`
	Page     int    `form:"page"`
	PageSize int    `form:"pageSize"`
}

// AnnouncementListResp 公告列表响应
type AnnouncementListResp struct {
	List     []AnnouncementItem `json:"list"`
	Total    int64              `json:"total"`
	Page     int                `json:"page"`
	PageSize int                `json:"pageSize"`
}

// AnnouncementItem 公告项
type AnnouncementItem struct {
	ID         uint       `json:"id"`
	Title      string     `json:"title"`
	Content    string     `json:"content"`
	Type       string     `json:"type"`
	Priority   string     `json:"priority"`
	IsTop      bool       `json:"isTop"`
	CoverImage string     `json:"coverImage"`
	ViewCount  int        `json:"viewCount"`
	PublishAt  *time.Time `json:"publishAt"`
	CreatedAt  time.Time  `json:"createdAt"`
}

// CreateAnnouncementReq 创建公告请求
type CreateAnnouncementReq struct {
	Title       string     `json:"title" binding:"required"`
	Content     string     `json:"content" binding:"required"`
	Type        string     `json:"type" binding:"required,oneof=NOTICE EVENT MAINTENANCE PROMOTION"`
	Priority    string     `json:"priority" binding:"required,oneof=LOW NORMAL HIGH URGENT"`
	TargetUsers string     `json:"targetUsers" binding:"required,oneof=ALL MEMBERS COACHES CUSTOM"`
	CoverImage  string     `json:"coverImage"`
	PublishAt   *time.Time `json:"publishAt"`
	ExpireAt    *time.Time `json:"expireAt"`
	IsTop       bool       `json:"isTop"`
}

// UpdateAnnouncementReq 更新公告请求
type UpdateAnnouncementReq struct {
	Title       string     `json:"title"`
	Content     string     `json:"content"`
	Type        string     `json:"type" binding:"oneof=NOTICE EVENT MAINTENANCE PROMOTION"`
	Priority    string     `json:"priority" binding:"oneof=LOW NORMAL HIGH URGENT"`
	TargetUsers string     `json:"targetUsers" binding:"oneof=ALL MEMBERS COACHES CUSTOM"`
	CoverImage  string     `json:"coverImage"`
	PublishAt   *time.Time `json:"publishAt"`
	ExpireAt    *time.Time `json:"expireAt"`
	IsTop       bool       `json:"isTop"`
}

// TemplateListReq 模板列表查询请求
type TemplateListReq struct {
	Page     int `form:"page"`
	PageSize int `form:"pageSize"`
}

// TemplateListResp 模板列表响应
type TemplateListResp struct {
	List     []Template `json:"list"`
	Total    int64      `json:"total"`
	Page     int        `json:"page"`
	PageSize int        `json:"pageSize"`
}

// CreateTemplateReq 创建模板请求
type CreateTemplateReq struct {
	Code      string   `json:"code" binding:"required"`
	Name      string   `json:"name" binding:"required"`
	Type      string   `json:"type" binding:"required,oneof=COURSE CARD SYSTEM OTHER"`
	Channel   string   `json:"channel" binding:"required,oneof=INTERNAL SMS EMAIL PUSH ALL"`
	Title     string   `json:"title" binding:"required"`
	Content   string   `json:"content" binding:"required"`
	Variables []string `json:"variables"`
	IsActive  bool     `json:"isActive"`
}

// UpdateTemplateReq 更新模板请求
type UpdateTemplateReq struct {
	Code      string   `json:"code"`
	Name      string   `json:"name"`
	Type      string   `json:"type" binding:"oneof=COURSE CARD SYSTEM OTHER"`
	Channel   string   `json:"channel" binding:"oneof=INTERNAL SMS EMAIL PUSH ALL"`
	Title     string   `json:"title"`
	Content   string   `json:"content"`
	Variables []string `json:"variables"`
	IsActive  bool     `json:"isActive"`
}

// NotificationSettingsResp 通知设置响应
type NotificationSettingsResp struct {
	Settings []NotificationSettingItem `json:"settings"`
}

// NotificationSettingItem 通知设置项
type NotificationSettingItem struct {
	Channel   string `json:"channel"`
	Type      string `json:"type"`
	IsEnabled bool   `json:"isEnabled"`
}

// UpdateSettingsReq 更新通知设置请求
type UpdateSettingsReq struct {
	Settings []NotificationSettingItem `json:"settings" binding:"required,min=1"`
}

// SendCourseReminderReq 发送课程提醒请求
type SendCourseReminderReq struct {
	BookingID    uint   `json:"bookingId" binding:"required"`
	UserID       uint   `json:"userId" binding:"required"`
	CourseName   string `json:"courseName" binding:"required"`
	ClassTime    string `json:"classTime" binding:"required"`
	ReminderType string `json:"reminderType" binding:"required,oneof=BOOKING_SUCCESS BEFORE_CLASS CANCELLED"`
}

// SendCardExpireReq 发送会员卡到期提醒请求
type SendCardExpireReq struct {
	CardID        uint   `json:"cardId" binding:"required"`
	UserID        uint   `json:"userId" binding:"required"`
	CardType      string `json:"cardType" binding:"required"`
	ExpireDate    string `json:"expireDate" binding:"required"`
	DaysRemaining int    `json:"daysRemaining" binding:"required"`
}

// SendAnnouncementReq 批量推送公告请求
type SendAnnouncementReq struct {
	AnnouncementID uint   `json:"announcementId" binding:"required"`
	TargetUsers    string `json:"targetUsers" binding:"required,oneof=ALL MEMBERS COACHES CUSTOM"`
}